/* Copyright 2014 Nate Eisner
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.uwp.kcwm;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.TileOverlayOptions;

public class Map extends FragmentActivity implements OnMarkerClickListener {
	// starting lat long zoom
	private static final double LAT = -36;
	private static final double LON = -1.5;
	private static final float ZOOM = 0;

	// Map
	private GoogleMap mMap = null;

	ImageView mapImage;
	ScrollView sView;

	/**
	 * Activity is created
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		getActionBar().hide();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sView = (ScrollView) findViewById(R.id.map_container);
		setUpMapIfNeeded();
		mapImage = (ImageView) findViewById(R.id.mapimg);
		sendScroll();
	}

	/**
	 * Activity is resumed
	 */
	@Override
	public void onResume() {
		super.onResume();
		setUpMapIfNeeded();
	}

	/**
	 * Check to see if we need to setup map
	 */
	private void setUpMapIfNeeded() {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (mMap == null) {
			// obtain the map from the MapFragment and attach Listeners
			SupportMapFragment cMapFragment = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map));

			mMap = cMapFragment.getMap();
			setUpMap();
		}
	}

	/**
	 * Setup mMap's type and other options
	 */
	public void setUpMap() {
		mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
		// adds tile overlay with a custom tile provider with local assets
		mMap.addTileOverlay(new TileOverlayOptions()
				.tileProvider(new CustomMapTileProvider(getResources()
						.getAssets())));
		// add exhibit markers
		addMarkers();
		CameraUpdate upd = CameraUpdateFactory.newLatLngZoom(new LatLng(LAT,
				LON), ZOOM);

		mMap.moveCamera(upd);

		// disable zoom controls
		mMap.getUiSettings().setZoomControlsEnabled(false);
		// disable compass
		mMap.getUiSettings().setCompassEnabled(false);
		// disable built in gestures, just use custom gestures
		mMap.getUiSettings().setAllGesturesEnabled(false);
		// Listener for map markers
		mMap.setOnMarkerClickListener(this);
	}

	/**
	 * Adds markers to the mMap object
	 */
	private void addMarkers() {
		// Exhibit Markers
		// 1
		MarkerOptions exhibit1 = new MarkerOptions()
				.position(new LatLng(-60, 0)).title("exhibit1")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit1);
		MarkerOptions exhibit2 = new MarkerOptions()
				.position(new LatLng(-64.5, 44)).title("exhibit2")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit2);
		MarkerOptions exhibit3 = new MarkerOptions()
				.position(new LatLng(-55.7, 40)).title("exhibit3")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit3);
		MarkerOptions exhibit4 = new MarkerOptions()
				.position(new LatLng(-45.5, 36)).title("exhibit4")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit4);
		MarkerOptions exhibit5 = new MarkerOptions()
				.position(new LatLng(-3.5, 37)).title("exhibit5")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit5);
		MarkerOptions exhibit6 = new MarkerOptions()
				.position(new LatLng(-16, -2)).title("exhibit6")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit6);
		MarkerOptions exhibit7 = new MarkerOptions()
				.position(new LatLng(-38.4, 0)).title("exhibit7")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit7);
		MarkerOptions exhibit8 = new MarkerOptions()
				.position(new LatLng(-1, -43)).title("exhibit8")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit8);
		MarkerOptions exhibit9 = new MarkerOptions()
				.position(new LatLng(-53, -42)).title("exhibit9")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit9);
		MarkerOptions exhibit10 = new MarkerOptions()
				.position(new LatLng(-64, -24)).title("exhibit10")
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
		mMap.addMarker(exhibit10);
	}

	/**
	 * Museum marker is clicked on map
	 */
	@Override
	public boolean onMarkerClick(Marker marker) {
		// loop through 10 exhibits
		for (int i = 1; i <= 10; i++) {
			String bigI = Integer.toString(i);
			if (marker.getTitle().equals("exhibit" + bigI)) {
				Intent intent = new Intent(Map.this, Exhibit.class);
				intent.putExtra("exhibitNum", bigI);
				Map.this.startActivity(intent);
			}
		}
		return (true);
	}

//	/**
//	 * For adding markers and locating clicks
//   *  FOR DEBUG USE ONLY	
//	 */
//	@Override
//	public void onMapClick(LatLng point) {
//		int duration = Toast.LENGTH_SHORT;
//
//		Toast toast = Toast.makeText(getApplicationContext(), point.toString(),
//				duration);
//		toast.show();
//	}

	/**
	 * Scrolls to the bottom of the scrollview
	 */
	private void sendScroll() {
		final Handler handler = new Handler();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
				}
				handler.post(new Runnable() {
					@Override
					public void run() {
						sView.fullScroll(View.FOCUS_DOWN);
					}
				});
			}
		}).start();
	}
}